package com.example.jd.finance.sever.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jd.common.DateUtils;
import com.example.jd.common.ExcelUtils;
import com.example.jd.common.InvokeResultUtil;
import com.example.jd.common.PageInfoUtil;
import com.example.jd.finance.entity.MsWechatTotalStatiscs;
import com.example.jd.finance.entity.param.DetailPageParam;
import com.example.jd.finance.entity.param.MsQueryEnterpriseWechatCodePageParam;
import com.example.jd.finance.entity.vo.MsCustomerRecordsVO;
import com.example.jd.finance.entity.vo.MsQueryDetailCountVO;
import com.example.jd.finance.mapper.MsWechatIncreaseCustomerDetailsMapper;
import com.example.jd.finance.entity.MsWechatIncreaseCustomerDetails;
import com.example.jd.finance.mapper.MsWechatTotalStatiscsMapper;
import com.example.jd.finance.sever.MsCustomerRecordsService;
import com.example.jd.finance.sever.MsWechatIncreaseCustomerDetailsService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-11-06
 */
@Service
public class MsWechatIncreaseCustomerDetailsServiceImpl extends ServiceImpl<MsWechatIncreaseCustomerDetailsMapper, MsWechatIncreaseCustomerDetails> implements MsWechatIncreaseCustomerDetailsService {

    @Autowired
    private MsWechatIncreaseCustomerDetailsMapper msWechatIncreaseCustomerDetailsMapper;

    @Autowired
    private MsCustomerRecordsService msCustomerRecordsService;

    @Autowired
    private MsWechatTotalStatiscsMapper msWechatTotalStatiscsMapper;

    @Override
    public InvokeResultUtil<Object> queryEnterpriseWechatCodePage(MsQueryEnterpriseWechatCodePageParam param) {

        Page page = new Page(param.getPageIndex(), param.getPageSize());
        List<MsQueryDetailCountVO> list = msWechatIncreaseCustomerDetailsMapper.queryEnterpriseWechatCodePage(page, param);
        if (CollectionUtils.isEmpty(list)) {
            return InvokeResultUtil.success(new PageInfoUtil<>());
        }
        long l = msWechatIncreaseCustomerDetailsMapper.queryEnterpriseWechatCodePageTotal(param);
        LocalDate today = LocalDate.now();
        if (!ObjectUtils.isEmpty(param.getCurrentDay())){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            today = LocalDate.parse(param.getCurrentDay(), fmt);
        }
        for (MsQueryDetailCountVO e : list) {
            MsQueryDetailCountVO v = msCustomerRecordsService.queryDetailCount(e.getEnterpriseWechatCode(),today);
            if (v != null) {
                e.setApplyNumCurrentDay(v.getApplyNumCurrentDay());
                e.setApplyPassedNumCurrentDay(v.getApplyPassedNumCurrentDay());
                e.setCountApplyNum(v.getCountApplyNum());
                e.setCountApplyPassedNum(v.getCountApplyPassedNum());
            }
        }
        PageInfoUtil<MsQueryDetailCountVO> pageN = new PageInfoUtil<>();
        pageN.setPageIndex(param.getPageIndex());
        pageN.setPageSize(param.getPageSize());
        pageN.setPagedItems(list);
        pageN.setTotalCount(l);
        return InvokeResultUtil.success(pageN);

    }


    @SneakyThrows
    @Override
    public MsQueryDetailCountVO queryEnterpriseWechatCodeStatistics() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 10,
                10L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));
        MsQueryDetailCountVO vo = new MsQueryDetailCountVO();


        Future<MsWechatTotalStatiscs> countApplyNum = threadPoolExecutor.submit(() ->
                msWechatTotalStatiscsMapper.getTodayStatiscs()
        );
        Future<MsQueryDetailCountVO> countApplyPassedNum = threadPoolExecutor.submit(() ->
                msWechatTotalStatiscsMapper.queryMsQueryDetailCountVO()
        );

        MsWechatTotalStatiscs m = countApplyNum.get();
        MsQueryDetailCountVO n = countApplyPassedNum.get();
        if (m != null){
            vo.setApplyNumCurrentDay(m.getCurrentApplyTotal());
            vo.setApplyPassedNumCurrentDay(m.getCurrentApplySuccessTotal());
        }
        if (n != null){
            vo.setCountApplyPassedNum(n.getCountApplyPassedNum());
            vo.setCountApplyNum(n.getCountApplyNum());
        }
        return vo;
    }

    @Override
    public Map<String, Object> enterprisePageExcel(MsQueryEnterpriseWechatCodePageParam param, HttpServletResponse response) {
        List<MsQueryDetailCountVO> list = msWechatIncreaseCustomerDetailsMapper.queryEnterpriseWechatCodeExcel(param);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        LocalDate today = LocalDate.now();
        if (!ObjectUtils.isEmpty(param.getCurrentDay())){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            today = LocalDate.parse(param.getCurrentDay(), fmt);
        }
        for (MsQueryDetailCountVO e : list) {
            MsQueryDetailCountVO v = msCustomerRecordsService.queryDetailCount(e.getEnterpriseWechatCode(), today);
            if (v != null) {
                e.setApplyNumCurrentDay(v.getApplyNumCurrentDay());
                e.setApplyPassedNumCurrentDay(v.getApplyPassedNumCurrentDay());
                e.setCountApplyNum(v.getCountApplyNum());
                e.setCountApplyPassedNum(v.getCountApplyPassedNum());
            }
        }
        //导出Excel
        ExcelUtils.writeExcel(response, list, MsQueryDetailCountVO.class, DateUtils.getMinString());
        return null;
    }

}
